package com.baykalcakici.cafepicker.dialog;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import com.baykalcakici.cafepicker.Main;
import com.baykalcakici.cafepicker.tasks.VisitTask;

/**
 * Represents the dialog after a user clicks on a cafe name.
 */
public class VisitDialog {
  public static void display(final String cafeName, final Context context) {
    final CharSequence[] menuOptions = {"Visit", "Menu", "Map", "Share"};
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(cafeName);
    builder.setItems(menuOptions, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int index) {
        switch (index) {
          case 0: // Visit
            ProgressDialog progressDialog =
                ProgressDialog.show(context, "", "Visiting. Please wait...", true);
            VisitTask visitTask = new VisitTask(progressDialog);
            visitTask.execute(cafeName);
            break;
          case 1: // Menu
            String url = Main.cafePicker.getCafeUrl(cafeName);
            final Intent menuIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
            context.startActivity(menuIntent);
            break;
          case 2 : // Map
            final Intent mapIntent =
                new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(String.format(
                    // "google.map:q=%s", Main.cafePicker.getCafeLocation(cafeName)))); // navigate
                    "geo:0,0?q=%s", Main.cafePicker.getCafeLocation(cafeName))));
            context.startActivity(mapIntent);
            break;
          case 3 : // Share
            final Intent shareIntent =
                new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(
                    android.content.Intent.EXTRA_TEXT, String.format("Going to %s", cafeName));
            context.startActivity(Intent.createChooser(shareIntent, "Share using"));
            break;
        }
      }
    });
    AlertDialog alert = builder.create();
    alert.show();
  }
}
